/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.info.ifinance;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

/**
 * @author YangHua
 *
 */
public class InfoManage {
	
	public static boolean checkNet(Context context){
		boolean result=false;
		ConnectivityManager cm=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netinfo = cm.getActiveNetworkInfo();
		if ( netinfo !=null&&netinfo.isConnected() ) {
			result=true;
			Log.i( context.getClass().getName(), "The net was connected" );
		}else{
			Toast.makeText(context,context.getResources().getText(R.string.dialog_message_net_not), Toast.LENGTH_SHORT).show();		
		}
		return result;
	}
	
	 public static void addInfoItem(InfoItem info,Context context) {
		 ContentValues values = new ContentValues();
		 values.put(InfoItem.INFO_COLUMN_INFO_TITLE, info.getTitle());
		 values.put(InfoItem.INFO_COLUMN_INFO_ID, info.getInfoId());
		 values.put(InfoItem.INFO_COLUMN_INFO_DATE, info.getPubDate());
		 values.put(InfoItem.INFO_COLUMN_INFO_LINK, info.getLink());
		 values.put(InfoItem.INFO_COLUMN_INFO_SUMMARY, info.getSummary());
		 values.put(InfoItem.INFO_COLUMN_INFO_STATUS, info.getStatus());
		 context.getContentResolver().insert(InfoItem.CONTENT_URI, values);
   	 }
	 
	 public static InfoItem findFirstInfo(Context context){
		 String selection=InfoItem.INFO_COLUMN_INFO_STATUS+" <> "+InfoConstant.STATUS_DEL
		 	+" and "+InfoItem.INFO_COLUMN_INFO_STATUS+" <> "+InfoConstant.STATUS_FAV;
		 String sortOrder=InfoItem.INFO_COLUMN_INFO_ID+" desc" ;
		 InfoItem info=null;
		 Cursor cur=context.getContentResolver().query(InfoItem.CONTENT_URI, InfoItem.COLUMNS, selection, null, sortOrder);
		 if(cur.moveToFirst()){
			info=getInfoItem(cur); 
		 }
		 return info;
	 }
	 
	 public static InfoItem findInfoItem(String infoId,Context context){
		 String selection=InfoItem.INFO_COLUMN_INFO_ID+" like '"+infoId+"'";
		 InfoItem info=null;
		 Cursor cur=context.getContentResolver().query(InfoItem.CONTENT_URI, InfoItem.COLUMNS, selection, null, null);
		 if(cur.moveToFirst()){
			info=getInfoItem(cur); 
		 }
		 return info;
	 }
	 
	 public static Cursor findAllInfo(Context context,String selection,String[]selectionArgs,String sortOrder){
		 return context.getContentResolver().query(InfoItem.CONTENT_URI, InfoItem.COLUMNS, selection, selectionArgs, sortOrder);
	 }
	 
	 public static void updateInfo(InfoItem info,Context context){
		 ContentValues values = new ContentValues();
		 values.put(InfoItem.INFO_COLUMN_INFO_STATUS, info.getStatus());
		 String where=InfoItem.INFO_COLUMN_ID+"="+info.getId();
		 context.getContentResolver().update(InfoItem.CONTENT_URI, values, where, null);
	 }
	 
	 public static void updateInfo(String selection,Context context){
		 ContentValues values = new ContentValues();
		 values.put(InfoItem.INFO_COLUMN_INFO_STATUS, InfoConstant.STATUS_DEL);
		 context.getContentResolver().update(InfoItem.CONTENT_URI, values, selection, null);
	 }
	 
	 public static void refreshInfo(String selection,Context context){
		 ContentValues values = new ContentValues();
		 values.put(InfoItem.INFO_COLUMN_INFO_STATUS, InfoConstant.STATUS_NEW);
		 context.getContentResolver().update(InfoItem.CONTENT_URI, values, selection, null);
	 }
	 
	 public static void delInfoItem(String date,Context context){
		 String where=InfoItem.INFO_COLUMN_INFO_DATE+"<'"+date+"' and "+
		 	InfoItem.INFO_COLUMN_INFO_STATUS+" <> "+InfoConstant.STATUS_FAV;
		 context.getContentResolver().delete(InfoItem.CONTENT_URI, where, null);
	 }
	 
	 public static boolean infoIsExit(InfoItem infoItem,Context context){
		 String selection=InfoItem.INFO_COLUMN_INFO_TITLE+" like '"+infoItem.getTitle()
		 	+"' or "+InfoItem.INFO_COLUMN_INFO_LINK+" like '"+infoItem.getLink()+"'";
		 return context.getContentResolver().query(InfoItem.CONTENT_URI, InfoItem.COLUMNS, selection, null, null).moveToFirst();
	 }
	 
	 public static InfoItem getInfoItem(Cursor cur){
    	 InfoItem infoItem=null;
    	 if(cur!=null){
	    	 int id = cur.getInt(cur.getColumnIndex(InfoItem.INFO_COLUMN_ID));
	         String title=cur.getString(cur.getColumnIndex(InfoItem.INFO_COLUMN_INFO_TITLE));
	         String infoId = cur.getString(cur.getColumnIndex(InfoItem.INFO_COLUMN_INFO_ID));
	         String date=cur.getString(cur.getColumnIndex(InfoItem.INFO_COLUMN_INFO_DATE));
	         String link=cur.getString(cur.getColumnIndex(InfoItem.INFO_COLUMN_INFO_LINK));
	         String summary=cur.getString(cur.getColumnIndex(InfoItem.INFO_COLUMN_INFO_SUMMARY));
	         int status=cur.getInt(cur.getColumnIndex(InfoItem.INFO_COLUMN_INFO_STATUS));
	         infoItem=new InfoItem(id,title,infoId,date,link,summary,status);
    	 }
         return infoItem;
     }
	
}
